package logic

import (
	"MyBlogv2/Servers/UserAction/internal/svc"
	"MyBlogv2/Servers/UserAction/userAction"
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/redisUtil"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type CancelLikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCancelLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CancelLikeLogic {
	return &CancelLikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CancelLikeLogic) CancelLike(in *UserAction.UserActionRequest) (*UserAction.CancelLikeResponse, error) {
	typeOfLike := int(in.Type)
	typeId := int(in.TypeId)
	userId := int(in.UserId)
	models.Db.Model(new(models.Like)).Where("user_id = ? and type = ? and type_id = ?", userId, typeOfLike, typeId).Delete(new(models.Like))
	if typeOfLike == define.LIKE_TYPE_ARTICLE {
		redisUtil.DelLikeCount(typeId)
	}

	return &UserAction.CancelLikeResponse{
		Code: int64(HttpCode.SUCCESS.GetCode()),
	}, nil
}
